//
//  SDRecordViewController.m
//  YYPHSpeedJump
//
//  Created by speedjump on 15/12/23.
//  Copyright © 2015年 sujiang. All rights reserved.
//

#import "SDRecordViewController.h"
#import "ShareCell.h"
#import "SDSendMaViewController.h"
#import "SendModel.h"
@interface SDRecordViewController ()<UMSocialUIDelegate>
{
    NSInteger page;
}
@end

@implementation SDRecordViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    page = 1;
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"晒";
    self.view.backgroundColor = kColors(239, 239, 244, 1);
      [self requestData];
    
    
}

- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)requestData
{
    // http://192.168.1.199:8080/speedJump/user.do?cmd=deal_list&u=43&deal_type=2
    NSString *deal_type =@"3";
    NSString *string = [NSString stringWithFormat:@"%@%@", kBaseUrl, kDealListUrl];
    NSDictionary *diction = @{@"u":[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"], @"deal_type":deal_type};
    [NetworkHandle getUpDataWithUrl:string class:NSClassFromString(@"SendModel") dictionary:diction firstKey:@"data" secondKey:@"list" success:^(NSArray *array) {
        if (page == 1) {
            [self.dataArr removeAllObjects];
        }
        [self.dataArr addObjectsFromArray:array];
        // NSLog(@"%ld",self.dataArr.count);
        if (self.dataArr.count == 0) {
            UILabel *lable = [[UILabel  alloc]initWithFrame:CGRectMake(0, 13, kScreenWidth, 12)];
            lable.text = @"你还没有买过东西，快去买点东西吧!";
            lable.font = [UIFont systemFontOfSize:17];
            lable.textAlignment = NSTextAlignmentCenter;
            [self.view addSubview:lable];
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(22*kWidthScale,81 ,332*kWidthScale , 44);
            [btn setTitle:@"立即前往" forState:UIControlStateNormal];
            btn.titleLabel.textColor = [UIColor whiteColor];
            btn.backgroundColor= kColors(218, 166, 46, 1);
            btn.layer.cornerRadius = 5;
            [btn addTarget:self action:@selector(goToBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:btn];
        }else
        {
            [self createView];
            if (self.dataArr.count >= 10 && self.sdRecordTV.footer == nil) {
                self.sdRecordTV.footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
            }
        }

        [self.sdRecordTV reloadData];
    }];
    
}
- (void)goToBtnAction:(UIButton*)btn
{
    self.tabBarController.selectedIndex = 0;

    self.tabBarController.tabBar.hidden = NO;

    [self.navigationController popViewControllerAnimated:YES];
    

}
- (void)createView
{
    self.sdRecordTV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64)];
    self.sdRecordTV.delegate = self;
    self.sdRecordTV.dataSource =self;
    [self.view addSubview:self.sdRecordTV];
    self.sdRecordTV.header = [MJRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
}

- (void)headerRefresh
{
    page = 1;
    [self requestData];
}

- (void)footerRefresh
{
    page++;
    [self requestData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShareCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShareCell"];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"ShareCell" owner:nil options:nil]lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.dataArr[indexPath.row];
        cell.shaiBtn.tag = 500+indexPath.row;
        [cell.shaiBtn addTarget:self action:@selector(purchaseBtnAct:) forControlEvents:UIControlEventTouchUpInside];
        cell.shareBtn.tag = 10+indexPath.row;
        [cell.shareBtn addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
        cell.deleteBtn.tag = 1000 + indexPath.row;
        [cell.deleteBtn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 139;
}
//删除
- (void)deleteBtnAction:(UIButton *)btn
{
    SendModel *model = self.dataArr[btn.tag - 1000];
    
    NSString *string = [NSString stringWithFormat:@"%@%@", kBaseUrl, @"/user.do?cmd=delete_deal"];
    NSDictionary *dict = @{@"u":[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"], @"deal_id":model.deal_id};
    [NetworkHandle getInfoWithUrl:string dictionary:dict success:^(NSDictionary *dictionary) {
        if ([dictionary[@"code"] integerValue] == 200) {
            [Tools showAlert:@"删除成功!"];
        }
        [self.dataArr removeObject:model];
        [self.sdRecordTV reloadData];
           }];

}
//晒单
- (void)purchaseBtnAct:(UIButton *)btn
{
    SendModel *model = self.dataArr[btn.tag - 500];
    if (model.status.integerValue == 6) {
        [Tools showAlert:@"本单已晒过"];
        return;
    }
    SDSendMaViewController *sdSendVC =[[SDSendMaViewController alloc]init];
    sdSendVC.name = model.name;
    sdSendVC.deal_id = model.deal_id;
    sdSendVC.price = model.buy_price;
    
    [self.navigationController pushViewController:sdSendVC animated:YES];
}
//分享
- (void)shareAction:(UIButton *)button
{
    
    NSString *contentText = [NSString stringWithFormat:@"%@,%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"share_word"],[[NSUserDefaults standardUserDefaults]objectForKey:@"share_url"]];                                                                                                                                                                                 
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"5699e81567e58e7cba000b92"
                                      shareText:contentText
                                     shareImage:nil
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,nil]
                                       delegate:nil];
   
}
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    //根据responseCode得到发送结果,如果分享成功
    if(response.responseCode == UMSResponseCodeSuccess)
    {
        // 分享成功返回积分!
        NSString *string = [NSString stringWithFormat:@"%@/user.do?cmd=share_integral", kBaseUrl];
        NSDictionary *dic = @{@"u":[[NSUserDefaults standardUserDefaults]objectForKey:@"user_id"]};
        [NetworkHandle getInfoWithUrl:string dictionary:dic success:^(NSDictionary *dictionary) {
            [Tools showAlert:@"恭喜您获得10分享积分!"];
        }];

    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
